package org.example.controller;


import jakarta.validation.constraints.Pattern;
import org.example.entity.PageBean;
import org.example.model.Article;
import org.example.service.ArticleService;
import org.example.utils.Result;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        Integer createUser = ThreadLocalUtil.getClaimsByKey("id");
        article.setCreateUser( createUser);
        articleService.create(article);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<PageBean<Article>> list (
            Integer pageIndex,
            Integer pageSize,
            @RequestParam(required = false) String title
    ) {
        PageBean<Article> list =  articleService.list(pageIndex, pageSize, title);

        return Result.success(list);
    }
}
